package com.module.organization.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;
import base.util.StringUtil;
import base.util.Tools;

import com.module.organization.vo.Credit;
import com.module.organization.vo.Creditimg;
import com.module.sys.LoginSession;

import common.Logger;

public class CreditDao extends GenericHibernateDao<Credit, Long>{
	private CreditDao(){}
	
	private static CreditDao dao;
	
	public synchronized static CreditDao getInstance(){
		if(dao == null )
			dao = new CreditDao();
		return dao;
	}

	/**
	 * 企业列表
	 * @param bean  用户实体bean
	 * @param start 数据起始值
	 * @param limit 查询数量
	 * @return Page
	 */
	 public Page getList(Session session,Credit bean,Integer start, Integer limit){
	    	Page page = new Page();
	        page.setStart(start);
	        page.setLimit(limit);
	        StringBuffer sb = new StringBuffer();
	    	StringBuffer condition = new StringBuffer();
			List<Object> params = new ArrayList<Object>();
			if (Tools.isValid(bean.getOrgName())) {
				condition.append(" and obj.orgName like ?");
				params.add("%"+bean.getOrgName() + "%");
			}
			if (Tools.isValid(bean.getHttp())) {
				condition.append(" and obj.http like ?");
				params.add("%"+bean.getHttp()+ "%");
			}
			if (Tools.isValid(bean.getRepresent())) {
				condition.append(" and obj.represent like ?");
				params.add("%"+bean.getRepresent()+ "%");
			}
			if (Tools.isValid(bean.getOrgId())) {
				condition.append(" and obj.orgId in (select orgId from Credit where orgId in ("+bean.getOrgId()+"))");
			}
	        sb.append(" from Credit as obj where 1=1");
	        sb.append(condition);
	        Query query = session.createQuery(sb.toString());
	        int i = 0;
			for (Object obj : params) {
				query.setParameter(i, obj);
				i++;
			}
	        query.setFirstResult(start).setMaxResults(limit);
	        
			Query totalQuery = session
			.createQuery("select count(*) from Credit obj where 1=1 "+ condition);
			int j = 0;
			for (Object obj : params) {
				totalQuery.setParameter(j, obj);
				j++;
			}
	        
	        page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
	        List list = new ArrayList();
	        List<Credit> voList = query.list();
	        for(Credit u:voList){
	        	List orgNameList = session.createQuery("select puo.orgName from Credit as puo,Credit obj where puo.orgId = obj.orgId and obj.orgId='"+u.getOrgId()+"'").list();
	     	   list.add(u);
	        }
			
	        page.setRows(list);
	        return page;
	    }
	 
	 	
	 	/**
	 	 * 根据id查找数据 
	 	 * @param session
	 	 * @param id
	 	 * @return
	 	 * @throws Exception
	 	 */
		public Credit queryById(Session session,Long id) throws Exception {
			Credit result = new Credit();
			StringBuilder sb = new StringBuilder();
			sb.append("from Credit obj where obj.id="+id);
			 Query query = session.createQuery(sb.toString());
			 List<Credit> voList = query.list();
			 CreditImgDao dao = CreditImgDao.getInstance();
			 List<Creditimg> list = dao.getList(session, id);
			 if(voList!=null&&(!voList.isEmpty()))
			 {
				 result = voList.get(0);
				 result.setItems(list);
			 }
			return result;
		}
		
		public Credit queryByOrgId(Session session,String orgId) throws Exception{
			StringBuffer sb = new StringBuffer();
			sb.append("from Credit obj where obj.orgId'"+orgId+"'");
			Credit ce = (Credit) session.createQuery(sb.toString());
			return ce;
		}
		public boolean getCreditqueryById(Session session, String creditqueryById)throws Exception { 
			try { 
			String sql = "from Credit where orgId = '" 
			+ creditqueryById + "'"; 
			Query q = session.createQuery(sql); 
			return q.list().size()>0?true:false; 
			} catch (Exception e) { 
			e.printStackTrace(); 
			return false; 
			} 
			}
		
//		public void saveCreditAndImg(Session session,Credit cr,List<Creditimg> listImg) throws Exception{
//			session.save(cr);
//			for (Creditimg creditimg : listImg) {
//				creditimg.setCreditId(cr.getId());
//				session.save(creditimg);
//			}
//		}
		public void saveCreditAndImg(Session session,Credit cr) throws Exception{
		session.save(cr);
		List<Creditimg> crimg = cr.getItems();
			for (Creditimg creditimg : crimg) {
				creditimg.setCreditId(cr.getId());
				session.save(creditimg);
			}
		}

		public Credit queryByName(Session session, String name) {
			StringBuffer sb = new StringBuffer();
			sb.append("from Credit obj where obj.name'"+name+"'");
			Query query = session.createQuery(sb.toString());
			 List<Credit> voList = query.list();
			 if(voList!=null&&(!voList.isEmpty()))
			 {
				 Credit ce = voList.get(0);
				 return ce;
			 }
			return null;
		}		
		
		
		
}
